package com.xc.hotelmgr.datapusherali.service.mybatis.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.hotelmgr.datapusherali.mapper.HtHotelinfoMapper;
import com.xc.hotelmgr.datapusherali.pojo.ali.hotel.S_hotel;
import com.xc.hotelmgr.datapusherali.pojo.ali.hotel.XhotelResponse;
import com.xc.hotelmgr.datapusherali.pojo.mybatis.HtHotelinfo;
import com.xc.hotelmgr.datapusherali.sdk.FzSdk;
import com.xc.hotelmgr.datapusherali.service.mybatis.HtHotelinfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("hotelinfoService")
public class HtHotelinfoServiceImpl extends ServiceImpl<HtHotelinfoMapper, HtHotelinfo> implements HtHotelinfoService {

    @Autowired
    private HtHotelinfoMapper hotelinfoMapper;

    @Autowired
    private FzSdk fzSdk;

    @Override
    public int updateExceptNull(HtHotelinfo htHotelinfo) {
        return hotelinfoMapper.updateIgnoreNull(htHotelinfo);
    }

    @Override
    public HtHotelinfo selectById(Long id) {
        return hotelinfoMapper.selectById(id);
    }

    @Override
    public HtHotelinfo selectAll(Long id) {
        return hotelinfoMapper.selectAllHotel(id);
    }

    @Override
    public HtHotelinfo selectAllHotelExceptPs(Long id) {
        return hotelinfoMapper.selectAllHotelExceptPs(id);
    }

    /**
     * 使用阿里酒店定下更新本地酒店信息
     * @param resultHotel
     */
    @Override
    public void hotelUpdateByAli(String resultHotel) {
        XhotelResponse xhotelResponse = JSONObject.parseObject(resultHotel,XhotelResponse.class);
        HtHotelinfo aliHotel = new HtHotelinfo();
        aliHotel.setId(Long.valueOf(xhotelResponse.getXhotel_add_response().getXhotel().getOuter_id()));
        aliHotel.setIsMatch(xhotelResponse.getXhotel_add_response().getXhotel().getMatch_status());
        S_hotel sHotel = xhotelResponse.getXhotel_add_response().getXhotel().getS_hotel();
        Long shid = 1L;
        String hotelName = "阿里旅行默认酒店";
        String address = "暂时没有地址";
        if(sHotel != null){
            if(sHotel.getShid() != null) {
                shid = sHotel.getShid();
            }
            if(StringUtils.isNotBlank(sHotel.getName())) {
                hotelName = sHotel.getName();
            }
            if(StringUtils.isNotBlank(sHotel.getAddress())){
                address = sHotel.getAddress();
            }
        }
        if(shid == 1L || hotelName.contains("阿里旅行默认酒店") || address.contains("暂时没有地址")){
            aliHotel.setIsMatch(2);
        }else {
            aliHotel.setShid(shid);
            aliHotel.setHotelNameAli(hotelName);
            if(StringUtils.isNotBlank(sHotel.getAddress())){
                aliHotel.setAddress(sHotel.getAddress());
            }
            if(StringUtils.isNotBlank(sHotel.getTel())){
                aliHotel.setPhone(sHotel.getTel());
            }
            if(sHotel.getPosition_type() != null){
                aliHotel.setAxes(sHotel.getPosition_type().toString());
            }
            if(StringUtils.isNotBlank(sHotel.getLongitude())){
                aliHotel.setAxesX(sHotel.getLongitude());
            }
            if(StringUtils.isNotBlank(sHotel.getLatitude())){
                aliHotel.setAxesY(sHotel.getLatitude());
            }
            if(StringUtils.isNotBlank(sHotel.getType())) {
                aliHotel.setAtype(sHotel.getType());
            }
            if(StringUtils.isNotBlank(sHotel.getBrand())) {
                aliHotel.setBrandName(sHotel.getBrand().substring(0,sHotel.getBrand().length()>300?299:sHotel.getBrand().length()));
            }
        }
        try{
            updateById(aliHotel);
        }catch (Exception ex){
            log.error(ex.toString());
        }

    }
}
